/**
 * @file tal_zigbee_local_auto_action.h
 * @brief This is tal_zigbee_local_auto_action file
 * @version 1.0
 * @date 2021-09-10
 *
 * @copyright Copyright 2021-2022 Tuya Inc. All Rights Reserved.
 *
 */

#ifndef __TAL_ZIGBEE_LOCAL_AUTO_ACTION_H__
#define __TAL_ZIGBEE_LOCAL_AUTO_ACTION_H__

#include "tuya_cloud_types.h"
#include "tuya_error_code.h"

#ifdef __cplusplus
extern "C" {
#endif

/***********************************************************************
 ********************* constant ( macro and enum ) *********************
 **********************************************************************/
#define MAX_RAW_OR_STRING_ACTION_DP_PARAMS_LEN 10   // 子动作，RAW 和 STRING 类型的参数最大为多长（由于没有 malloc, 采用预留最大可能的设置）

#define ACTION_IS_NOT_EXEC  0           // 动作没有在执行
#define ACTION_IS_EXEC      1           // 动作在执行


/***********************************************************************
 ********************* struct ******************************************
 **********************************************************************/
#pragma pack (1)
typedef struct {
    UINT8_T kind;
    union {
        struct {                         // 动作类型是 DP
            UINT8_T dpid;
            UINT8_T dpkind;
            UINT8_T op;
            UINT8_T params[MAX_RAW_OR_STRING_ACTION_DP_PARAMS_LEN+1];
        } dp;
        struct {                         // 动作类型是延时
            UINT16_T timeout_s;              // 超时时间
            UINT16_T left_time_s;            // 剩余时间
        } delay;
        struct {                         // 动作类型是自动化
            UINT16_T auto_id;
            UINT8_T op;
        } ato;
    };
} TAL_ZIGBEE_LOCAL_AUTO_ACTION_T;          // max = 15bytes

                                        // 一个条件序列
typedef struct {
    struct {
        UINT8_T rev:3;                       // 保留
        UINT8_T exec_flag:1;                 // 动作是否正在执行
        UINT8_T num:4;                       // 子动作数量
    } attr;                             // 条件序列属性
    UINT8_T curr_exec_action_index;          // 每个联动当前正在/将要被执行的动作
} TAL_ZIGBEE_LOCAL_AUTO_ACTION_LIST_T;     // 2
#pragma pack ()

typedef  VOID_T (*tal_zigbee_local_auto_action_cb_p)(UINT16_T autoid, UINT8_T dpid, UINT8_T dpkind, UINT8_T op ,UINT8_T *datas, UINT8_T datas_len);
typedef  VOID_T (*__local_auto_action_broadcast_cb_p)(UINT16_T autoid, UINT8_T op);
/***********************************************************************
 ********************* variable ****************************************
 **********************************************************************/


/***********************************************************************
 ********************* function ****************************************
 **********************************************************************/
/**
 * @brief tal_zigbee_local_auto_action_init
 *
 * @param[in] param: none
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
UINT8_T tal_zigbee_local_auto_action_init(VOID_T);

/**
 * @brief etal_zigbee_local_auto_action_set_callback
 *
 * @param[in] cb: cb
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
UINT8_T tal_zigbee_local_auto_action_set_callback(tal_zigbee_local_auto_action_cb_p cb);

/**
 * @brief tal_zigbee_local_auto_action_with_broadcast_callback
 *
 * @param[in] cb: cb
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
UINT8_T tal_zigbee_local_auto_action_with_broadcast_callback(__local_auto_action_broadcast_cb_p cb);

/**
 * @brief tal_zigbee_local_auto_action_engin
 *
 * @param[in] param: none
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
UINT8_T tal_zigbee_local_auto_action_engin(VOID_T);

/**
 * @brief tal_zigbee_local_auto_action_check
 *
 * @param[in] *params: *params
 * @param[in] params_len: params_len
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
UINT8_T tal_zigbee_local_auto_action_check(UINT8_T *params, UINT8_T params_len);


#ifdef __cplusplus
}
#endif

#endif /* __TAL_ZIGBEE_LOCAL_AUTO_ACTION_H__ */

